<?php
class Photo {
	public static function update($post_data)
	{
		require_once(ML_LIBS."/phpthumb/ThumbLib.inc.php");
		
		if (isset($post_data[Protocol::ID])) {
			// On Update
			$data[Protocol::ID] 		= $post_data[Protocol::ID];
			$data[Protocol::VIEWS]		= $post_data[Protocol::VIEWS];
			$data[Protocol::LIKES]		= $post_data[Protocol::LIKES];
		}
		else {
			// On Create
			$data[Protocol::CREATED_TIME] = time();
		}

		$data[Protocol::MODEL_ID]		= $post_data[Protocol::MODEL_ID];
		$data[Protocol::TAGS]			= $post_data[Protocol::TAGS];
		$data[Protocol::DESCRIPTION]	= $post_data[Protocol::DESCRIPTION];
		$data[Protocol::MODIFIED_TIME]	= time();
		
		if (isset($_FILES[Protocol::FILE]["name"]) && !empty($_FILES[Protocol::FILE]["name"])) {
			$filename = time()."-".basename($_FILES[Protocol::FILE]["name"]);
			$filepath = ML_MEDIA."/$filename";
			$fileurl = substr(ML_MEDIA, strlen(ML_PUBLIC))."/$filename";
					
			if (move_uploaded_file($_FILES[Protocol::FILE]["tmp_name"], $filepath)) {
				$thumb = PhpThumbFactory::create($filepath);
				$thumb->adaptiveResize(ML_THUMB_B_WIDTH, ML_THUMB_B_HEIGHT);
				$thumb->save(Util::filename_add_suffix($filepath, "-b"));
				$thumb->adaptiveResize(ML_THUMB_S_WIDTH, ML_THUMB_S_HEIGHT);
				$thumb->save(Util::filename_add_suffix($filepath, "-s"));
				
				if (isset($post_data[Protocol::ID])) self::delete_media($post_data[Protocol::ID]);
				
				$data[Protocol::FILE] = $fileurl;
				$data[Protocol::THUMB_B] = Util::filename_add_suffix($fileurl, "-b");
				$data[Protocol::THUMB_S] = Util::filename_add_suffix($fileurl, "-s");
			} else {
				Log::error("Failed to upload a file ".$_FILES[Protocol::FILE]["name"]);
				return FALSE;
			}
		}
		
		return DBConnector::update(
			Protocol::PHOTO, $data
		);
	}
	
	public static function get_list(
	$start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE, $where = FALSE)
	{
		$fields = array(
			Protocol::ID, Protocol::MODEL_ID, Protocol::THUMB_B, Protocol::THUMB_S,
			Protocol::TAGS, Protocol::CREATED_TIME,	Protocol::VIEWS, Protocol::LIKES
		);
		$search_fields = array(Protocol::NAME, Protocol::TAGS, Protocol::DESCRIPTION);
		return DBConnector::get_list(
			Protocol::PHOTO, $start, $end, $search_fields, $keyword, $order, $fields, FALSE, $where);
	}
	
	public static function get_list_by_model_id(
	$model_id, $start = FALSE, $end = FALSE, $keyword = FALSE, $order = FALSE)
	{
		$where = Protocol::MODEL_ID." = $model_id";
		return self::get_list($start, $end, $keyword, $order, $where);
	}
	
	public static function get($value, $key = FALSE)
	{
		if ($key === FALSE) $key = Protocol::ID;
		return DBConnector::get(Protocol::PHOTO, $key, $value);
	}
	
	public static function delete($id_list)
	{
		foreach ($id_list as $id) self::delete_media($id);
		return DBConnector::delete(Protocol::PHOTO, $id_list);
	}
	
	private static function delete_media($id)
	{
		$photo = Photo::get($id);
		if (isset($photo[Protocol::FILE])) unlink(ML_PUBLIC.$photo[Protocol::FILE]);
		if (isset($photo[Protocol::THUMB_B])) unlink(ML_PUBLIC.$photo[Protocol::THUMB_B]);
		if (isset($photo[Protocol::THUMB_S])) unlink(ML_PUBLIC.$photo[Protocol::THUMB_S]);
	}
}
